#include "Including.h"



using namespace std;


namespace MD2Models
{

	bool flag = false;

	/**
	 * Render the model at frame n.
	 */
	void MyMD2Models::RenderFrame (int n, const struct md2_model_t *mdl)
	{
		int i, j;
		vec3_t v;
		struct md2_frame_t *pframe;
		struct md2_vertex_t *pvert;

		// Check if n is in a valid range
		if ((n < 0) || (n > mdl->header.num_frames - 1))
			return;

		// Enable model's texture
		//glBindTexture (GL_TEXTURE_2D, mdl->tex_id);


		//---------TEXTURING---------//
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, texture2);
		glEnable(GL_TEXTURE_GEN_S);
		glEnable(GL_TEXTURE_GEN_T);
		glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
		glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);

		/* Draw the model */
		glPushMatrix();
		glTranslatef(12,0,-20);
		glRotatef(230,0,1,0);
		glRotatef(-90,1,0,0);
		glScalef(0.2,0.2,0.2);
		glBegin (GL_TRIANGLES);
		// Draw each triangle
		for (i = 0; i < mdl->header.num_tris; ++i)
		{
			// Draw each vertex 
			for (j = 0; j < 3; ++j)
			{
				pframe = &mdl->frames[n];
				pvert = &pframe->verts[mdl->triangles[i].vertex[j]];

				// Calculate vertex real position 
				v[0] = (pframe->scale[0] * pvert->v[0]) + pframe->translate[0];
				v[1] = (pframe->scale[1] * pvert->v[1]) + pframe->translate[1];
				v[2] = (pframe->scale[2] * pvert->v[2]) + pframe->translate[2];
				glVertex3fv (v);
			}
		}
		glEnd ();	
		glPopMatrix();
		glDisable(GL_TEXTURE_2D);

	}



	/**
	 * Render the model with interpolation between frame n and n+1.
	 * interp is the interpolation percent. (from 0.0 to 1.0)
	 */
	void MyMD2Models::RenderFrameItp (int n, float interp, const struct md2_model_t *mdl)
	{
		int i, j;
		vec3_t v_curr, v_next, v;
		struct md2_frame_t *pframe1, *pframe2;
		struct md2_vertex_t *pvert1, *pvert2;

		// Check if n is in a valid range
		if ((n < 0) || (n > mdl->header.num_frames))
			return;

		// Enable model's texture 
		//glBindTexture (GL_TEXTURE_2D, mdl->tex_id);

		//---------TEXTURING---------//
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, texture2);
		//glEnable(GL_TEXTURE_GEN_S);
		//glEnable(GL_TEXTURE_GEN_T);
		//glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
		//glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);

	
		// Draw the model 
		glPushMatrix();
		glBegin (GL_TRIANGLES);
	    // Draw each triangle 
	    for (i = 0; i < mdl->header.num_tris; ++i)
	      {
			// Draw each vertex 
			for (j = 0; j < 3; ++j)
			  {
				pframe1 = &mdl->frames[n];
				pframe2 = &mdl->frames[n + 1];
				pvert1 = &pframe1->verts[mdl->triangles[i].vertex[j]];
				pvert2 = &pframe2->verts[mdl->triangles[i].vertex[j]];
	
				// Interpolate vertices
				v_curr[0] = pframe1->scale[0] * pvert1->v[0] + pframe1->translate[0];
				v_curr[1] = pframe1->scale[1] * pvert1->v[1] + pframe1->translate[1];
				v_curr[2] = pframe1->scale[2] * pvert1->v[2] + pframe1->translate[2];

				v_next[0] = pframe2->scale[0] * pvert2->v[0] + pframe2->translate[0];
				v_next[1] = pframe2->scale[1] * pvert2->v[1] + pframe2->translate[1];
				v_next[2] = pframe2->scale[2] * pvert2->v[2] + pframe2->translate[2];
	
			    v[0] = v_curr[0] + interp * (v_next[0] - v_curr[0]);
			    v[1] = v_curr[1] + interp * (v_next[1] - v_curr[1]);
			    v[2] = v_curr[2] + interp * (v_next[2] - v_curr[2]);

				glVertex3fv (v);
			}
		}
		glEnd ();
		glPopMatrix();
		//glDisable(GL_TEXTURE_GEN_S);
		//glDisable(GL_TEXTURE_GEN_T);
		glDisable(GL_TEXTURE_2D);

	}


	/**
	 * Calculate the current frame in animation beginning at frame
	 * 'start' and ending at frame 'end', given interpolation percent.
	 * interp will be reseted to 0.0 if the next frame is reached.
	 */
	void MyMD2Models::Animate (int start, int end, int *frame, float *interp)
	{
		if ((*frame < start) || (*frame > end))
		*frame = start;

		if (*interp >= 1.0f)
		{
			if(!flag)
			{
				*interp = 0.0f;
				(*frame)++;	//Move to next frame
				if (*frame >= end)
				{
					*frame = start;
					//flag = true;
				}
			}
/*			else
			{ 
				*interp = 0.0f;
				(*frame)--;	
				if (*frame >= start)
				{
					++(*frame);
					flag = false;
					cout<<flag<<endl;
				}
			}
*/
		}
	}


}